package com.android.multiplesmart.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.multiplesmart.R;
import com.android.multiplesmart.bean.MianReBean;

import java.util.List;

/**
 * 展现对话内容的adapter
 */
public class MainAdapter  extends RecyclerView.Adapter<MainAdapter.MainViewHolder> {

    private Context context;
    private List<MianReBean> mData;
    private String username;
    public MainAdapter(Context context, List<MianReBean> mData,String username) {
        this.context = context;
        this.mData = mData;
        this.username=username;
    }

    public  void  setmData( List<MianReBean> mData){
        this.mData=mData;

    }
    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if(viewType==1){
             view = LayoutInflater.from(context).inflate(R.layout.items,  parent, false);
        }else {
            view = LayoutInflater.from(context).inflate(R.layout.item,  parent, false);
        }
        return new MainViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
              System.out.println(mData.get(position).getName()+"获取数据"+mData.get(position).getName().length());
//            holder.timess.setText(mData.get(position).getTime());
              SpannableString spannableString = new SpannableString(mData.get(position).getName()+":"+mData.get(position).getMessage());
              spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#FFF0B4")), 0,mData.get(position).getName().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//            tv.setText(spannableString);
              holder.name.setText(spannableString);
//            holder.text.setText(mData.get(position).getMessage());
    }

    @Override
    public int getItemViewType(int position) {
        if (mData.get(position).getType()=="1") {
            return 1;
        }else {
            return 2;
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class   MainViewHolder extends RecyclerView.ViewHolder{
        TextView timess;
        TextView name;
        TextView text;
        public MainViewHolder(@NonNull View itemView) {
            super(itemView);
            timess=itemView.findViewById(R.id.timess);
            name= itemView.findViewById(R.id.name);
            text= itemView.findViewById(R.id.text);
        }

    }

}
